package com.link2room.rodimus.facade.listener.receiver;

import com.link2room.aeriabots.dto.result.base.WxMemberInfoResDto;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.util.StringUtil;
import com.link2room.rodimus.constant.SignInConstant;
import com.link2room.rodimus.dto.param.base.SignInBaseDto;
import com.link2room.rodimus.dto.param.base.SignInReissueHandlerParamDto;
import com.link2room.rodimus.entity.MemberBase;
import com.link2room.rodimus.feign.aeriabots.WxMemberInfoFeignService;
import com.link2room.rodimus.handler.ISignInDrawReissueHandler;
import com.link2room.rodimus.listener.event.NewMemberEvent;
import com.link2room.rodimus.service.IMemberService;
import com.link2room.rodimus.service.ISignInService;
import com.link2room.rodimus.util.CalendarUtils;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 会员注册接收器
 * Created by LN on 2018/1/10
 */
@Component
public class NewMemberEventReceiver {
    @Resource
    private ISignInDrawReissueHandler signInDrawReissueHandler;
    @Resource
    private IMemberService memberService;
    @Resource
    private WxMemberInfoFeignService wxMemberInfoFeignService;
    @Resource
    private ISignInService signInService;
    /**
     * 签到助力分享处理
     * @param shareEvent
     * @throws InterruptedException
     */
    @EventListener(value = NewMemberEvent.class)
    @Async("newMember")
    public void signInShareHandler(NewMemberEvent shareEvent) throws Exception {
        Date month = CalendarUtils.getStartTimeOfMonth(new Date());
        String from = shareEvent.getFrom();
        if(StringUtil.isBlank(from)){
            return;
        }
        String memberNo = shareEvent.getMemberNo();
        String headUrl = null;
        String nickName = null;
        if(StringUtil.isBlank(memberNo)){
            return;
        }
        String lhotelGroupCode = shareEvent.getLhotelGroupCode();
        MemberBase memberBase = memberService.selectMemberBaseByNo(lhotelGroupCode, memberNo);
        if(null == memberBase){
            return;
        }

        SignInBaseDto signInBase = signInService.getUnderwaySignInBaseAll(lhotelGroupCode);
        if(DefaultValue.F.equals(signInBase.getCouldReissue())){
            return;
        }



        if(StringUtil.isNotBlank(memberBase.getOpenid())){
            BaseResultDto<WxMemberInfoResDto> wxMemberInfro = wxMemberInfoFeignService.getMemberInfos(lhotelGroupCode, memberBase.getOpenid());
            headUrl =  wxMemberInfro.getResultInfo().getHeadimgurl();
            nickName = wxMemberInfro.getResultInfo().getNickname();
        }


        SignInReissueHandlerParamDto paramDto = new SignInReissueHandlerParamDto();
        paramDto.setFrom(from);
        paramDto.setMonth(month);
        paramDto.setMemberNo(memberNo);
        paramDto.setHeadUrl(headUrl);
        paramDto.setNickName(nickName);
        paramDto.setLhotelGroupCode(lhotelGroupCode);
        signInDrawReissueHandler.signInReissue(SignInConstant.ReissueType.SHAER,paramDto);
    }
}
